from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters, status

from restful_api.common.serializer import *
from utils.unite.generic import *
from utils.unite.result import ApiResponse, DEFAULT_CODE_VALIDATE, get_serializer_error, DEFAULT_CODE_ERROR

"""
    @TODO 任何用户都可请求
"""


class WxAdminLogin(CreateAPIView):
    """
        【账号】管理员微信登陆
    """
    serializer_class = WxLoginSerializer
    queryset = User.objects.filter(is_active=True)

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            _username = serializer.validated_data.get('username') or None
            user = None
            admin = None
            try:
                user = User.objects.get(username=_username)
                admin = ManagerProfile.objects.get(user=user)
            except User.DoesNotExist:
                return ApiResponse(message=u"找不到用户", code=DEFAULT_CODE_ERROR)
            except ManagerProfile.DoesNotExist:
                return ApiResponse(message=u"对象不是管理员", code=DEFAULT_CODE_ERROR)

            content = {
                "username": user.username,
                "nickname": admin.nickname,
                "mobile": admin.mobile,
                "avatar": admin.avatar,
            }
            headers = self.get_success_headers(serializer.data)
            return ApiResponse(data=serializer.data, message=u"登陆成功", status=status.HTTP_200_OK, headers=headers)
        else:
            return ApiResponse(code=DEFAULT_CODE_VALIDATE, message=get_serializer_error(serializer.errors),
                               data=serializer.errors)


class WxUserLogin(CreateAPIView):
    """
        【账号】用户微信登陆
    """
    serializer_class = WxLoginSerializer
    queryset = User.objects.filter(is_active=True)

    def post(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid():
            _username = serializer.validated_data.get('username') or None
            user = None
            user_profile = None
            try:
                user = User.objects.get(username=_username)
                user_profile = UserProfile.objects.get(user=user)
            except User.DoesNotExist:
                return ApiResponse(message=u"找不到用户", code=DEFAULT_CODE_ERROR)
            except ManagerProfile.DoesNotExist:
                return ApiResponse(message=u"对象不是管理员", code=DEFAULT_CODE_ERROR)

            content = {
                "username": user.username,
                "nickname": user_profile.nickname,
                "mobile": user_profile.mobile,
                "avatar": user_profile.avatar,
            }
            headers = self.get_success_headers(serializer.data)
            return ApiResponse(data=serializer.data, message=u"登陆成功", status=status.HTTP_200_OK, headers=headers)
        else:
            return ApiResponse(code=DEFAULT_CODE_VALIDATE, message=get_serializer_error(serializer.errors),
                               data=serializer.errors)


class GetGoodsCategoryListView(ListAPIView):
    """
        【商品-分类】列表
    """
    serializer_class = GetGoodsCategoryListSerializer
    queryset = GoodsCategory.objects.filter(is_active=True).order_by("-sort")


class GetGoodsListView(ListAPIView):
    """
        【商品】列表
    """
    serializer_class = GetGoodsListSerializer
    queryset = Goods.objects.filter(is_active=True).order_by("-is_top", "sort")

    filter_backends = (filters.SearchFilter, DjangoFilterBackend, filters.OrderingFilter,)

    filter_fields = (
        'category',
    )

    search_fields = (
        'name',
    )

    ordering_fields = (
        'is_top',
        'sort',
    )


class GetGoodsDetailView(RetrieveAPIView):
    """
        【商品】详情
    """
    serializer_class = GetGoodsDetailSerializer
    queryset = Goods.objects.filter(is_active=True)


class GetBusinessActivityListView(ListAPIView):
    """
        【活动】列表
    """
    serializer_class = GetBusinessActivityListSerializer
    queryset = BusinessActivity.objects.filter(is_active=True)


class GetBusinessActivityDetailView(RetrieveAPIView):
    """
        【活动】详情
    """
    serializer_class = GetBusinessActivityDetailSerializer
    queryset = BusinessActivity.objects.filter(is_active=True)


class GetBusinessNoticeListView(ListAPIView):
    """
        【商家-通告】列表
    """
    serializer_class = GetBusinessNoticeListSerializer
    queryset = BusinessNotice.objects.filter(is_active=True)


class GetBusinessNoticeDetailView(RetrieveAPIView):
    """
        【商家-通告】详情
    """
    serializer_class = GetBusinessNoticeDetailSerializer
    queryset = BusinessNotice.objects.filter(is_active=True)
